<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8 grid-content">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:tdgg:save']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:tdgg:delete']">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="name" width="80" :show-overflow-tooltip="true" />
      <el-table-column label="停电类别" align="center" prop="type" :show-overflow-tooltip="true" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="结束时间" align="center" prop="endTime" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="星期" align="center" prop="week" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="停电线路" align="center" prop="info" width="155" :show-overflow-tooltip="true" />
      <el-table-column label="影响区域" align="center" prop="area" width="80" :show-overflow-tooltip="true" />

      <el-table-column label="操作" align="center" width="155" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:tdgg:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal='false' :close-on-press-escape='false'>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入公司名称" />
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="停电类别" prop="type">
              <el-select v-model="form.type" placeholder="系统内置" clearable size="small">
                <el-option v-for="dict in typeOptions" :key="dict.id" :label="dict.value" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="停电时间" prop="dateRange">
              <el-date-picker v-model="form.dateRange" size="small" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['12:00:00']" :style="{width: '100%' }" @change="dateChange"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="停电线路" prop="info">
          <el-input v-model="form.info" placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item label="影响区域" prop="area">
          <el-input v-model="form.area" placeholder="请输入影响区域" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  powerDelete,
  powerDetail,
  powerList,
  powerAdd,
} from "@/api/content/poweroff";
import beautify from "js-beautify";
export default {
  name: "Config",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [
        {
          id: 1,
          value: "计划停电",
        },
        { id: 2, value: "停电检修" },
      ],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configName: undefined,
        configKey: undefined,
        configType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        info: [
          { required: true, message: "参数键名不能为空", trigger: "blur" },
        ],
        type: [
          {
            required: true,
            message: "停电类别不能为空",
            trigger: ["blur", "change"],
          },
        ],
        name: [
          { required: true, message: "公司名称不能为空", trigger: "blur" },
        ],
        dateRange: [
          {
            required: true,
            message: "停电时间不能为空",
            trigger: ["blur", "change"],
          },
        ],
        area: [
          { required: true, message: "影响区域不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      powerList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.configList = response.data.list;
          this.total = response.data.totalCount;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.dateRange = [];
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.names = selection.map((item) => item.name);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getConfig(id).then((response) => {
        this.form = response.data;

        // let res = beautify(response.data.configValue, {});
        // this.form.configValue = res;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      function week(day) {
        let a = new Date(day).getDay();
        switch (a) {
          case 1:
            return "一";
            break;
          case 2:
            return "二";
            break;
          case 3:
            return "三";
            break;
          case 4:
            return "四";
            break;
          case 5:
            return "五";
            break;
          case 6:
            return "六";
            break;
          case 0:
            return "日";
            break;
          default:
            break;
        }
      }
      let a = new Date(this.dateRange[0]).toLocaleDateString();
      let b = new Date(this.dateRange[1]).toLocaleDateString();

      let c = week(this.dateRange[0]);
      let d = week(this.dateRange[1]);
      if (a !== b) {
        this.form.week = "星期" + c + "至" + "星期" + d;
      } else {
        this.form.week = "星期" + d;
      }
      let data = this.addDateRange(this.form, this.dateRange);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateConfig(data).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            powerAdd(data).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(",");
      const names = row.name || this.names.join(",");
      this.$confirm(
        '是否确认删除公司名称 " ' + names + ' " 的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return powerDelete({ id });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    dateChange() {
      this.dateRange = this.form.dateRange;
    },
  },
};
</script>